#Copyright (C) 2013 Mestrelab Research S.L. All rights reserved.
#
#This file is part of the Mnova scripting toolkit.
#
#Authorized users of Mnova Software may use this file freely, but this file is provided AS IS
#with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
#FOR A PARTICULAR PURPOSE.

import urllib.request
import sys

if len(sys.argv) < 5:
	sys.exit("Wrong number of parameters passed")

aURL = sys.argv[1]
aAction = sys.argv[2]
aSoapRequestFile = sys.argv[3]
aResponseFile = sys.argv[4]

try:
	with open(aSoapRequestFile, 'r') as reqFile:
		reqData = reqFile.read().encode('UTF-8')
	req = urllib.request.Request(url = aURL, data = reqData)
	req.add_header('Content-Type', 'application/soap+xml')
	req.add_header('SOAPAction', aAction)
	resp = urllib.request.urlopen(req)
	content = resp.read()
	with open(aResponseFile, 'wb') as respFile:
		respFile.write(content)
except Exception as e:
	sys.exit(e)